from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

from app.exchange import exchange_proxy

def yksp_invoke(country_code, phone_num, phone_code_url, exchange_code):
    proxy_info = exchange_proxy.proxy_method();
    options = webdriver.FirefoxOptions()
    options.add_argument(f'--proxy-server=http://{proxy_info[0]}:{proxy_info[1]}')

    driver = webdriver.Firefox(options=options)
    driver.get("https://www.youku.com/ku/webhome")
    driver.maximize_window()

    # 关闭弹层
    try:
        time.sleep(1)
        close_tag = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CLASS_NAME, 'full_close')))
        time.sleep(1)
        driver.execute_script('arguments[0].click();', close_tag)
    except Exception as e:
        print('弹层消失了')

    # 弹出登陆页
    time.sleep(5)
    avatar_tag = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.CLASS_NAME, 'crmusercenter_user_center_box')))
    avatar_tag.click()

    # 选择验证码登陆
    time.sleep(5)

    driver.execute_script("document.getElementsByClassName('login-tabs-tab')[1].click();")

    # login_tag = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'login')))
    # agree_tag = driver.find_element(By.ID, 'fm-agreement-checkbox')
    # driver.execute_script('arguments[0].click();', agree_tag)
    # agree_tag.click()


if __name__ == '__main__':
    yksp_invoke(1, 1, 1, 1)